<template>
  <view class="container">
    <!-- 有商品列表 -->
    <view>
      <view class="goodsList">
        <!--    <u-swipe-action>
          <u-swipe-action-item @click="delCheck(item)" :options="options1" v-for="item in list" :key="item.id"> -->
        <u-swipe-action v-for="(item, index) in list" @click="delCheck(item)" :key="item.id" :options="options1">
          <view class="a-goods">
            <view class="content">
              <!-- 有库存且在售才可以讨论勾不勾选 -->
              <view class="checkbox">
                <!-- 选中状态-->
                <image v-show="item.checked" @tap="stateFns(item)" class="checkbox-img" src="/static/lzl/gou-red.png"></image>
                <!-- 未选中状态 -->
                <image v-show="!item.checked" @tap="stateFns(item)" class="checkbox-img" src="/static/lzl/gou-gray.png"></image>
              </view>
              <!-- 没有库存，直接勾选灰色 -->
              <!-- <image src="/common/gou-gray.png" class="checkbox"></image> -->
              <view class="goods-info">
                <navigator class="goods-url" hover-class="none" url="">
                  <view class="img-box"><image :src="item.goods.image" class="img" /></view>
                  <view class="text-box">
                    <view class="goods-title out-stock-title">{{ item.goods.title }}</view>
                    <view class="goods-label">{{ item.sku_price.goods_sku_text ? item.sku_price.goods_sku_text : '' }}</view>
                    <!--      <view class="goods-price">
                      <view class="price-now">￥{{ item.price }}</view>
                    </view> -->
                    <view class="goods-price">
                      <view style="color: #000000;font-size: 30rpx;font-weight: 600;">￥{{ item.goods.price }}</view>
                    </view>
                    <!-- 购买数量计算器 -->
                    <view class="selnum">
                      <view class="cut" @click="changeNum(item, 0)">-</view>
                      <input :value="item.goods_num" class="number" disabled="true" type="number" />
                      <view class="add" @click="changeNum(item, 1)">+</view>
                    </view>
                    <!-- 缺货描述 -->
                    <!-- <view  class="out-stock">暂时缺货</view> -->
                  </view>
                </navigator>
              </view>
            </view>
          </view>
        </u-swipe-action>
        <!--        </u-swipe-action-item>
        </u-swipe-action> -->
      </view>
      <!-- 没有商品信息 -->
      <!--    <view class="cart-empty-container">
        <view class="cart-empty-view"><image class="cart-empty" src="/static/lzl/cart-empty.png"></image></view>
        <view class="cart-empty-txt">车里什么都没有，快去买一点吧</view>
        <view class="to-index-btn" @click="stroll">去逛逛</view>
      </view> -->
    </view>
    <!-- 没有登录 -->
    <!-- <u-empty mode="permission" text="您还没有登录哦" v-else></u-empty> -->
    <!-- 底部区域 -->
    <view class="settle-box">
      <view class="left-price">
        <view class="all-selected">
          <!-- 选中状态 -->
          <image v-if="state" @click.stop="seletYes" class="checkbox" src="/static/lzl/gou-red.png"></image>
          <!-- 未选中状态 -->
          <image v-else @click.stop="seletNo" class="checkbox" src="/static/lzl/gou-gray.png"></image>
          <view class="text">全选({{ SelectedQuantity }})</view>
        </view>
        <view class="total">合计:¥{{ picnum }}</view>
      </view>
      <view class="to-pay-btn" @click="setlement">结算</view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      // state: true,
      statShow: true, //购物车选中状态
      price: '',
      num: 1,
      token: '',
      userShopping: [],
      options1: [
        {
          text: '删除',
          style: {
            backgroundColor: '#f56c6c'
          }
        }
      ],
      list: [
        {
          id: 1,
          type: true,
          num: 2,
          title: '乐伊卡通针织衫 蓝色/灰色 印花针织',
          sku: 'A0A19C 中灰；L',
          price: '118.00'
        },
        {
          id: 2,
          type: false,
          num: 2,
          title: '乐伊卡通针织衫 蓝色/灰色 印花针织',
          sku: 'A0A19C 中灰；L',
          price: '118.00'
        }
      ]
    };
  },
  onShow() {
    // 请求数据
    this.init();
    this.token = uni.getStorageSync('token');
  },
  computed: {
    // 价格
    picnum() {
      let num = 0;
      this.list.forEach(item => {
        if (item.checked) {
          num += item.goods_num * item.goods.price;
        }
      });
      return num.toFixed(2);
    },
    state() {
      return this.list.every(item => item.checked);
    },
    // 商品数量
    SelectedQuantity() {
      let m = 0;
      this.list.forEach(item => {
        if (item.checked) {
          m += item.goods_num * 1;
        }
      });
      return m;
    }
  },
  methods: {
    // 点击去结算
    async setlement() {
      // 筛选出为true的数据
      let payList = [];
      let n = this.list.filter(item => {
        let obj = {};
        if (item.checked) {
          obj.goods_id = item.goods_id;
          obj.goods_num = item.goods_num;
          obj.goods_price = item.sku_price.price;
          obj.sku_price_id = item.sku_price_id;
        }
        payList.push(obj);
      });
      let m = { from: 'cart', goods_list: payList };
      uni.navigateTo({
        url: '/pages/Shopping/ConfirmOrder/ConfirmOrder?obj=' + JSON.stringify(m)
      });
    },
    //点击＋-
    async changeNum(item, val) {
      //说明是减操作
      if (val == 0) {
        if (item.goods_num <= 1) return;
        item.goods_num--;
        await this.$api.edit({
          act: 'change',
          cart_list: [item.id],
          value: item.goods_num
        });
        this.init();
        // 说明是＋
      } else {
        console.log(item);
        item.goods_num++;
        await this.$api.edit({
          act: 'change',
          cart_list: [item.id],
          value: item.goods_num
        });
        this.init();
      }
    },
    // 右滑删除
    async delCheck(data) {
      // 提示是不是要删除
      let [error, result] = await uni.showModal({
        content: '确定要删除吗？',
        title: '提示'
      });
      // 点击取消什么也不做
      if (result.cancel) return;
      // 点击确认就删除
      console.log(1111, '删除');
      await this.$api.edit({
        act: 'delete',
        cart_list: [data.id],
        value: ''
      });
      this.init();
    },
    // 点击切换状态
    stateFns(item) {
      // console.log(item);
      item.checked = !item.checked;
    },
    // 点击全选
    seletYes() {
      this.state = false;
      this.list.forEach(item => (item.checked = false));
    },
    //点击不全选
    seletNo() {
      this.state = true;
      this.list.forEach(item => (item.checked = true));
    },
    // 如果购物车没有商品显示去逛逛,当点击去逛逛后
    stroll() {
      uni.switchTab({
        url: '/pages/index/index'
      });
    },
    // 获取购物车列表
    async init() {
      let n = await this.$api.cart();
      n.data.forEach(item => {
        item.checked = true;
      });
      this.list = n.data;
      console.log(n);
    }
  }
};
</script>

<style lang="scss">
page {
  height: 100%;
  background-color: #fafafa;
}

.container {
  justify-content: initial;
  padding-bottom: 50rpx;
}

.cart-empty-container {
  padding-top: 240rpx;
  margin-bottom: 40rpx;
}

.show {
  display: block;
}

.cart-empty-view {
  width: 110rpx;
  height: 110rpx;
  margin: 0 auto;
}

.cart-empty {
  width: 110rpx;
  height: 110rpx;
}

.cart-empty-txt {
  width: 100%;
  text-align: center;
  color: #999;
  font-size: 28rpx;
  margin: 40rpx 0;
}

.to-index-btn {
  color: #fff;
  background: linear-gradient(to right, #01cb89, #01cb89);
  border-radius: 10rpx;
  width: 400rpx;
  height: 90rpx;
  line-height: 90rpx;
  text-align: center;
  font-size: 28rpx;
  margin: 0 auto;
}

.goodsList {
  margin-bottom: 100rpx;
}

.a-goods {
  width: 100%;
  height: 240rpx;
  overflow: hidden;
  position: relative;
  background: #fff;
}

.content {
  width: 100%;
  margin-right: 0;
  -webkit-transition: all 0.4s;
  transition: all 0.4s;
  -webkit-transform: translateX(90px);
  transform: translateX(90px);
  margin-left: -90px;
  height: 240rpx;
  overflow: hidden;
  display: flex;
  justify-content: flex-start;
}

.a-goods .checkbox {
  display: block;
  height: 40rpx;
  min-width: 40rpx;
  padding: 100rpx 40rpx;
}

.a-goods .checkbox .checkbox-img {
  height: 40rpx;
  width: 40rpx;
}

.goods-info {
  border-bottom: 1px solid #eee;
  width: 100%;
  padding: 30rpx 0 30rpx;
  box-sizing: border-box;
}

.a-goods:last-child .goods-info {
  border-bottom: none;
}

.goods-info .goods-url {
  width: 100%;
  height: auto;
  display: flex;
  justify-content: space-between;
}

.goods-info .img-box {
  width: 180rpx;
  height: 180rpx;
  overflow: hidden;
  margin-right: 20rpx;
  background-color: #fafafa;
}

.goods-info .text-box {
  width: 440rpx;
  position: relative;
}

.goods-info .text-box .goods-title {
  font-size: 30rpx;
  color: #233445;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
  padding: 10rpx 20rpx 5rpx 0;
}

.goods-info .text-box .out-stock-title {
  font-size: 30rpx;
  color: #000;
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
  padding: 10rpx 20rpx 5rpx 0;
}

.goods-info .text-box .goods-label {
  font-size: 24rpx;
  color: #aaa;
  height: 38rpx;
  line-height: 38rpx;
  margin: 8rpx 0 0rpx 0;
  margin-bottom: 30rpx;
}

/* .goods-info .text-box .goods-price{
  
  } */

.price-now {
  display: inline-block;
  font-size: 28rpx;
  font-weight: 700;
  color: #ff3456;
  margin-right: 10rpx;
}

.price-original {
  display: inline-block;
  color: #999;
  /* height: 40rpx; */
  font-size: 26rpx;
  /* float: left; */
  /* line-height: 44rpx; */
  text-decoration: line-through;
}

.goods-info .text-box .buy-num {
  width: 164rpx;
  height: 52rpx;
  line-height: 47rpx;
  position: absolute;
  right: 30rpx;
  bottom: 0;
  display: flex;
  /*justify-content: space-between;*/
  border: 1px solid #ccc;
  font-size: 26rpx;
  text-align: center;
}

.goods-info .text-box .out-stock {
  width: 164rpx;
  height: 52rpx;
  line-height: 52rpx;
  position: absolute;
  right: 30rpx;
  bottom: 0;
  border: 1px solid #fafafa;
  font-size: 26rpx;
  text-align: center;
  background: #fafafa;
}

.buy-num .minus-btn {
  width: 90rpx;
  height: 100%;
  text-align: center;
  line-height: 48rpx;
  color: #555;
  border-radius: 0;
  background: #fff;
  border: none;
  padding: 0;
}

.buy-num .number {
  width: 100rpx;
  height: 100%;
  text-align: center;
  line-height: 52rpx;
  border-left: 1px solid #ccc;
  border-right: 1px solid #ccc;
  float: left;
  font-size: 30rpx;
}

.buy-num .add-btn {
  width: 90rpx;
  height: 100%;
  text-align: center;
  line-height: 48rpx;
  color: #555;
  border-radius: 0;
  background: #fff;
  border: none;
  padding: 0;
}

button::after {
  border-radius: 0;
  border: none;
}

.goods-info .text-box .selnum {
  width: 146rpx;
  height: 52rpx;
  display: flex;
  justify-content: space-around;
  align-items: center;
  position: absolute;
  right: 30rpx;
  bottom: 0;
}

.selnum .cut {
  width: 46rpx;
  height: 46rpx;
  font-size: 30rpx;
  color: #233445;
  background: #fff;
  padding: 0;
  margin: 0;
  text-align: center;
  line-height: 40rpx;
  border-radius: 46rpx;
  border: 1rpx solid #999;
  box-sizing: border-box;
  font-weight: bold;
}

.selnum .add {
  width: 46rpx;
  height: 46rpx;
  font-size: 32rpx;
  color: #fff;
  padding: 0;
  margin: 0;
  text-align: center;
  line-height: 42rpx;
  background: #000;
  border-radius: 46rpx;
  box-sizing: border-box;
  font-weight: bold;
}

.selnum .number {
  width: 52rpx;
  height: 46rpx;
  font-size: 30rpx;
  color: #233445;
  text-align: center;
  line-height: 46rpx;
}

.goods-info .img-box .img {
  width: 180rpx;
  height: 180rpx;
}

.inner {
  position: absolute;
  top: 0;
}

.a-goods .delete-btn {
  background-color: #ff3456;
  width: 90px;
  height: 240rpx;
  text-align: center;
  line-height: 240rpx;
  color: #fff;
  -webkit-transform: translateX(90px);
  transform: translateX(90px);
  -webkit-transition: all 0.4s;
  transition: all 0.4s;
  position: absolute;
  top: 0;
  right: 0;
  font-size: 30rpx;
}

.touch-move-active .content,
.touch-move-active .delete-btn {
  -webkit-transform: translateX(0);
  transform: translateX(0);
}

.settle-box {
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  width: 90%;
  border-radius: 50rpx;
  height: 100rpx;
  position: fixed;
  bottom: 5%;
  left: 5%;
  border: 2px solid #000;
  // background-color: #fff;
  z-index: 999;
}

.settle-box .to-pay-btn {
  width: 300rpx;
  text-align: center;
  line-height: 100rpx;
  border-radius: 50rpx;
  background-color: #000;
  font-size: 32rpx;
  color: #fff;
}

.settle-box .to-pay-btn.no-select {
  background-color: #ccc;
}

.settle-box .left-price {
  display: flex;
  width: 600rpx;
  /* justify-content: space-between; */
  line-height: 100rpx;
  padding: 0 30rpx 0 0;
  font-size: 28rpx;
  box-sizing: border-box;
}

.settle-box .all-selected {
  padding-left: 20rpx;
  color: #000;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.settle-box .all-selected .checkbox {
  width: 40rpx;
  height: 40rpx;
  margin-right: 10rpx;
}

.settle-box .total {
  color: #ff3456;
  float: left;
  margin-left: 30rpx;
}
</style>
